const fs = require('fs');
const stat = fs.stat;

const copy = function (src, dst) {
  //读取目录
  var paths = fs.readdirSync(src);
  // console.log(paths)
  paths.forEach(function (path) {
    var _src = src + '/' + path;
    var _dst = dst + '/' + path;
    var readable;
    var writable;
    var st = fs.statSync(_src);

    if (st.isFile()) {
      readable = fs.createReadStream(_src);//创建读取流
      writable = fs.createWriteStream(_dst);//创建写入流
      readable.pipe(writable);
    } else if (st.isDirectory()) {
      exists(_src, _dst, copy);
    }
  });
}

const exists = function (src, dst, callback) {

  //测试某个路径下文件是否存在
  var exists2 = fs.existsSync(dst);
  if (exists2) {
    callback(src, dst);
  } else {//存在
    var flag = fs.mkdirSync(dst);//创建目录
    // console.log(flag, '123');
    if (!flag) {
      callback(src, dst)
    } else {
      throw '创建目录错误';
    }
  }
}

const writefile = function(topath, filename, filecontent){
  fs.writeFile(topath + "/" + filename, filecontent, function (err) {
    if (err) {
      return console.log(err);
    }
    console.log("The file was saved!");
  });
}

const activityMap = {
  'game': 'game',
  'h5': 'h5'
}
const activityName = 'Engineering'

module.exports = {
  activityMap: activityMap,
  activityName: activityName,
  copyfile: copy,
  exists: exists,
  writefile: writefile
}